If
If <.expression.> Then <.code.> | If <.expression.> ... [Else ...] EndIf
 
Parameters: NONE
Returns: NONE
 
     The If command is as decisional statement. It allows a program to execute a piece of code selectively. If is used in conjunction with either the Then statement, or the Else, ElseIf and EndIf statements.

      If statement blocks also using Comparisons and compare Operators, so you might was to check those out too.



I N D E X:





If / Then


      The pairing of the If/Then statements is the simplest form of single line decision possible. It allows us to check If a condition was true), then perform an action if it was. So when the If condition is True(1) the code following the Then statement is executed. However, when the condition is false(0), the code following the Then will be skipped over.


Examples

  
; Simple Examples ...  The conditions of the expressions here
;  will be all be TRUE, so the code after Then WILL BE executed.
  
  If  100 = 100 Then Print "Yes 100 does equal 100"
  
  If  50 > 10 Then Print " Yes 50 is bigger than 10"
  
  If  (100 * 5> 200 Then Print " Yes 100*5  is bigger than 200"
  
  Score= 100
  If Score > 50 Then Print "Score is bigger than 50"
  
  Lives =3
  If Lives >0 Then Print "Player Still Has some Lives Left"
  
  
; Simple Examples ...  The conditions of the expressions here
;  will be all be FALSE, so the code after Then is ignored.
  
  Score= 100
  If Score > 1000 Then Print "Score is bigger than 1000"
  Lives =3
  If Lives =0 Then Print "Player has no more Lives left"
  
  Sync
  WaitKey
  


Top





If / EndIf


      The pairing of the If/EndIf statements is the multi line form of the If/Then code above. So this combination allows for many lines of code to be selectively executed when the If's condition expression is met. Apart from being able to be used across many lines, that's the only difference to the If/Then form above.

Example

  
  lives=10
  If  Lives> 0
     Print "Play is not dead"
     Print "Keep on playing !"
  EndIf
  Sync
  WaitKey
  


Top





If / Else / EndIf


      Previously with the If / Then and If / EndIf combinations, we've only been able to selectively execute code when the If condition expression is TRUE. This is were Else allows us greater control. It allows us to choose the path our code takes, much like when driving a car at traffic intersection. So we can now choose between two TRUE/FALSE paths.

      So it's a little more complicated, but not too much!

      So logically, when the If condition is TRUE, the code between the If and Else statements will be executed. But when If the condition is FALSE, the code after the Else and between the Else-EndIf is executed.

Example

  
  Lives = 3
  If Lives > 0
     Print "Player Still has some lives left !"
  Else
     Print "Player is DEAD"
  EndIf
  Print "Done"
  Sync
  WaitKey
  


     This example would display (bellow), since the condition (lives > 0) is True. Notice how the code between the Else/EndIf is not executed. Only the code between the If/Else will be executed.

  
  Player Still has some lives left !
  Done
  


Top






If / ElseIf / Else / EndIf


      Previously we've seen that the If / Else / EndIf combination, allows us to selectively execute code based upon whether the If condition is TRUE or FALSE.

      While If / Else / EndIf can be very powerful combination, sometimes we'll need even more control than it offer.

      For example when the IF expression is FALSE, we might need to perform another IF comparison to find the match we're after. Doing this with IF/Else/EndIF statements requires us to nest If / EndIF statements, inside other If statements. Which can get confusing to read.

      To demonstrate this, bellow i've nested three IF/Endif statements inside each other. The code is trying catch and execute the correct response depending upon what value is stored in the 'Weapon' Variable.

Example

  
  Weapon = 2
  If Weapon =1
     Print "Player is using the Small Gun"
  Else
     If Weapon =2
        Print "Player is using the Big Gun"
     Else
        If Weapon =3
           Print "Player is using the Massive Gun"
        EndIf
     EndIf
  EndIf
  Print "Done"
  Sync
  WaitKey
  


      This example would output the following (bellow) when executed. Now since the Weapon variable equals 2, the first IF statement we hit will be FALSE as Weapon does no equal 1. Because it's False, execution will jump to the ELSE statement of the first IF. Here it will perform another IF comparison. This time the if statement will be TRUE as Weapon does indeed equal 2. Because the condition is met. Execution will run the code following that IF, then it'll jump out to that If statement to it's closing/matching EndIF statement.

      So the 3rd IF statement in this example, which has been nested inside the second If statement. Is Never even considered.

  
  Player is using the Big Gun
  Done
  


      While that's a perfectly acceptable approach, It can be a tad confusing to follow. This is where the ElseIF statement can make our code much easier to read.

     ElseIf acts much like the Else statement, where if the opening If condition is FALSE, execution skips ahead to the ElseIF statement like we'd expect. But ElseIf can also evaluate a conditional expression just like the If statement. This Allows us to test multiple the conditions, without having to nest If/EndIF statements. You can can have as many ElseIF statements in a row as you like. When you're finished, you close the statement with a single EndIF

Example


  
  Weapon = 2
  If Weapon =1
     Print "Player is using the Small Gun"
  ElseIf Weapon =2
     Print "Player is using the Big Gun"
  ElseIf Weapon =3
     Print "Player is using the Massive Gun"
  EndIf
  Print "Done"
  
  Sync
  WaitKey
  


      This example would output the following (bellow) when executed. Now since the 'Weapon' variable equals 2, when we hit the IF statement, the comparison will be FALSE since Weapon does not equal 1. Since it's False, execution will jump to the first ElseIF statement. Here it will perform another comparison, comparing Weapon with 2 this time. This statement will be TRUE, so the code following this ElseIF is executed. Once execution is complete, it will jump out and over any following code to the closing EndIF statement.

      So just like the previous example, the Second ElseIF statement Is never even considered.


  
  Player is using the Big Gun
  Done
  


Top




 
Related Info: #IF | Comparisons | Else | ElseIf | EndIf | Operators | Select | SwapIfHigher | SwapIfLower | Then :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com